package com.mtsmda.jpa.eclipseLink.vogella.run;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.mtsmda.jpa.eclipseLink.vogella.domain.ToDo;
import com.mtsmda.jpa.eclipseLink.vogella.domain.oneToMany.Family;
import com.mtsmda.jpa.eclipseLink.vogella.domain.oneToMany.PersonFamily;

public class FamilyRun {

	public static void main(String[] args) {
		EntityManager entityManager = Persistence.createEntityManagerFactory(
				"MTSMDA_EL").createEntityManager();
		entityManager.getTransaction().begin();

		Family family = new Family("Jonsons");

		PersonFamily personFamily = new PersonFamily("Jonson", "John");
		PersonFamily personFamily1 = new PersonFamily("Jonson", "Phil");
		PersonFamily personFamily2 = new PersonFamily("Jonson", "Jastin");
		personFamily.setFamily(family);
		personFamily1.setFamily(family);
		personFamily2.setFamily(family);

		family.getPersonFamilies().add(personFamily);
		family.getPersonFamilies().add(personFamily1);
		family.getPersonFamilies().add(personFamily2);
		
		entityManager.persist(family);

		entityManager.getTransaction().commit();

		System.out.println("\n*********\n");
		Query query = entityManager.createQuery("select f from Family f");
		List<Family> resultList = query.getResultList();
		System.out.println(resultList.size());
		System.out.println("\n*********\n");

		entityManager.close();
	}

}